package com.klangner.math.parser.javacc;

import junit.framework.TestCase;

import com.klangner.math.ast.Node;
import com.klangner.math.parser.javacc.JavaccParser;

public class ExtParserTestCase extends TestCase {

	public void testSimple() {
		Node document;
		JavaccParser parser = new JavaccParser( "2" );
		
		document = parser.parse();
		
		assertEquals("2", document.getText());
		assertEquals("2", document.toString());
	}

	public void testSum1() {
		Node document;
		JavaccParser parser = new JavaccParser( "3+2" );
		
		document = parser.parse();
		
		assertEquals("+", document.getText());
		assertEquals("3", document.getFirstChild().getText());
		assertEquals("2", document.getSecondChild().getText());

		assertEquals("3+2", document.toString());
	}

	public void testSum2() {
		Node document;
		JavaccParser parser = new JavaccParser( "3-2" );
		
		document = parser.parse();
		
		assertEquals("-", document.getText());
		assertEquals("3", document.getFirstChild().getText());
		assertEquals("2", document.getSecondChild().getText());

		assertEquals("3-2", document.toString());
	}

	public void testSum3() {
		Node document;
		JavaccParser parser = new JavaccParser( "3-2+4" );
		
		document = parser.parse();
		
		assertEquals("+", document.getText());
		assertEquals("-", document.getFirstChild().getText());
		assertEquals("4", document.getSecondChild().getText());

		assertEquals("3-2+4", document.toString());
	}


	public void testSum4() {
		Node document;
		JavaccParser parser = new JavaccParser( "3+2-4" );
		
		document = parser.parse();
		
		assertEquals("-", document.getText());
		assertEquals("+", document.getFirstChild().getText());
		assertEquals("4", document.getSecondChild().getText());

		assertEquals("3+2-4", document.toString());
	}


	public void testMultiply1() {
		Node document;
		JavaccParser parser = new JavaccParser( "3*2" );
		
		document = parser.parse();
		
		assertEquals("*", document.getText());
		assertEquals("3", document.getFirstChild().getText());
		assertEquals("2", document.getSecondChild().getText());

		assertEquals("3*2", document.toString());
	}

	public void testMultiply2() {
		Node document;
		JavaccParser parser = new JavaccParser( "3*-2" );
		
		document = parser.parse();
		
		assertEquals("*", document.getText());
		assertEquals("3", document.getFirstChild().getText());
		assertEquals("-", document.getSecondChild().getText());

		assertEquals("3*-2", document.toString());
	}

	public void testMultiply3() {
		Node document;
		JavaccParser parser = new JavaccParser( "-3*2" );
		
		document = parser.parse();
		
		assertEquals("*", document.getText());
		assertEquals("-", document.getFirstChild().getText());
		assertEquals("2", document.getSecondChild().getText());

		assertEquals("-3*2", document.toString());
	}

	public void testAddMultiply1() {
		Node document;
		JavaccParser parser = new JavaccParser( "3*2+4*5" );
		
		document = parser.parse();
		
		assertEquals("+", document.getText());
		assertEquals("*", document.getFirstChild().getText());
		assertEquals("*", document.getSecondChild().getText());

		assertEquals("3*2+4*5", document.toString());
	}

	public void testAddMultiply2() {
		Node document;
		JavaccParser parser = new JavaccParser( "3+2*4-5" );
		
		document = parser.parse();
		
		assertEquals("-", document.getText());
		assertEquals("+", document.getFirstChild().getText());
		assertEquals("5", document.getSecondChild().getText());

		assertEquals("3+2*4-5", document.toString());
	}

}
